package com.icss.ah.postTrade.action;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.regex.Pattern;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.icss.ah.base.action.BaseAction;
import com.icss.ah.base.bean.SyncSelectTree;
import com.icss.ah.base.util.Escape;
import com.icss.ah.login.bean.UserInfoBean;
import com.icss.ah.postTrade.service.PostTradeService;

@Controller
public class PostTradeAction extends BaseAction{
	
	private static final long serialVersionUID = 4956348725903791158L;
	private final Logger errlog = Logger.getLogger("E");
	private final Logger infolog = Logger.getLogger("I");
	
	@Autowired
	private PostTradeService postTradeService;
	
	private String level;
	private String itemCode;
	private String itemName;
	private String viewCode;
	private String codeName;
	private String flag;
	private String typePCode;//店铺类别父类型编码
	private String productId;//商品编码
	private String viewFlag;//查询标记
	private String areaCode;//销售区域
	
	/**
	 * 发布商品首页
	 * @return
	 */
	public String toPostTradeOnePage(){
		return "toPostTradeOnePage";
	}
	
	/**
	 * 页面选择类目列表查询
	 */
	public void getCategoryList(){
		JSONObject json = new JSONObject();
		try {
			Map<String, String> map = new HashMap<String, String>();
			map.put("level", level);
			map.put("itemCode", itemCode);
			map.put("itemName", itemName);
			List<Map<String, Object>> list = postTradeService.getCategoryList(map);
			json.put("list", list);
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("PostTradeAction|getCategoryList：",e);
		} finally {
			str2Resp(json.toString());
		}
	}
	
	/**
	 * 页面搜索列表查询
	 */
	public void queryCategoryList(){
		JSONObject json = new JSONObject();
		try {
			Map<String, String> map = new HashMap<String, String>();
			map.put("itemName", itemName);
			List<Map<String, String>> list = postTradeService.getCategoryNameList(map);
			json.put("list", list);
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("PostTradeAction|queryCategoryList：",e);
		} finally {
			str2Resp(json.toString());
		}
	}
	
	/**
	 * 发布商品第二页面
	 * @return
	 */
	public String toPostTradeTwoPage(){
		String areaName = "";
		try {
			itemName = new String(itemName.getBytes("ISO-8859-1"),"UTF-8");
			if(itemName!=null&&itemName.indexOf("%")>=0){
				itemName = Escape.unescape(itemName);
			}
			String userCode=getUserCodeFromSession();
			UserInfoBean userInfo=getUserInfo(userCode);
			String corpId = userInfo.getCorpId();
			String shopId=userInfo.getShopId();
			Map<String, String> map = new HashMap<String, String>();
			map.put("corpId", corpId);
			map.put("shopId", shopId);
			map.put("productId", "0");
			List<Map<String, Object>> areaList = postTradeService.queryShopAreaList(map);
			if(areaList.size() > 0){
				for (Map<String, Object> aMap : areaList) {
					areaCode += (String) aMap.get("AREA_CODE").toString() + ",";
					areaName += (String) aMap.get("ANAME").toString() + ",";
				}
				if(!"".equals(areaCode)){
					areaCode = areaCode.substring(0,areaCode.length() - 1);
					areaName = areaName.substring(0,areaName.length() - 1);
				}
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			areaName = "请先设定店铺销售区域";
		}
		itemName = itemName.replace(",", ">>");
		request = getRequest();
		request.setAttribute("itemName", itemName);
		request.setAttribute("itemCode", itemCode);
		request.setAttribute("areaCode", areaCode);
		request.setAttribute("areaName", areaName);
		return "toPostTradeTwoPage";
	}
	
	/**
	 * 商品参数查询
	 */
	public void queryBaseParam(){
		JSONObject json = new JSONObject();
		try {
			Map<String, String> map = new HashMap<String, String>();
			map.put("itemCode", itemCode);
			List<Map<String, Object>> list = postTradeService.getBaseParam(map);
			json.put("baseParaList", list);
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("PostTradeAction|queryBaseParam：",e);
		} finally {
			str2Resp(json.toString());
		}
		
	}
	
	/**
	 * 下拉列表字典查询
	 */
	public void queryDirectoryInfo(){
		JSONObject json = new JSONObject();
		try {
			Map<String, String> map = new HashMap<String, String>();
			map.put("viewCode", viewCode);
			map.put("codeName", codeName);
			map.put("viewFlag", viewFlag);
			List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
			if("1".equals(flag)) list = postTradeService.getDirectoryInfo(map);
			else list = postTradeService.getMeasuringInfo(); //计量单位
			json.put("directoryList", list);
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("PostTradeAction|queryDirectoryInfo：",e);
		} finally {
			str2Resp(json.toString());
		}
	}
	
	/**
	 * 店铺类别查询
	 */
	public void getShopType(){
		JSONObject json = new JSONObject();
		try {
			String typeFlag = getRequest().getParameter("typeFlag");
			Map<String, String> map = new HashMap<String, String>();
			String userCode = getUserCodeFromSession();
			map.put("userCode", userCode);
			String corpId = getBaseCorpId(); //从配置文件里取公众号id
			map.put("corpId", corpId);
			map.put("level", level);
			map.put("typePCode", typePCode);
			map.put("typeFlag", typeFlag);
			List<Map<String, Object>> list = postTradeService.getShopType(map);
			json.put("shopTypeList", list);
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("PostTradeAction|getShopType：",e);
		} finally {
			str2Resp(json.toString());
		}
		
	}
	
	public void save(){
		JSONObject json = new JSONObject();
		try {
			request = getRequest();
			Map<String, String> map = new HashMap<String, String>();
			String productId = UUID.randomUUID().toString();//商品编码
			String userCode=getUserCodeFromSession();
			UserInfoBean userInfo=getUserInfo(userCode);
			String corpId = userInfo.getCorpId();
			String shopId=userInfo.getShopId();
			String postName = request.getParameter("postName");//商品名称
			String brandCode = request.getParameter("brandCode");//品牌名称
			String categoryCode = request.getParameter("itemCode");//商品类目
			String shopTypeCode = request.getParameter("shopTypeCode");//店内商品分类
			String postDesc = request.getParameter("postDesc");//商品描述
			String measuring = request.getParameter("measuring");//计价单位
			String unitWeight = request.getParameter("unitWeight");//单位重量
			String delDays = request.getParameter("sendDate");//发货日期
			String allowReturn = request.getParameter("allowReturn");//是否允许退换货
			String buyerRange = request.getParameter("buyerRange");//买家范围
			allowReturn = null != allowReturn && "是".equals(allowReturn) ? "1" : "0";
			buyerRange = null != buyerRange && "全部".equals(buyerRange) ? "1" : "2";
			String reg = "[\u4e00-\u9fa5]";
			delDays = Pattern.compile(reg).matcher(delDays).replaceAll("");
			String proStock = request.getParameter("proStock");//商品库存
			proStock = proStock == null ? "0" : proStock;
			String proPrice = request.getParameter("proPrice");//成本价格
			proPrice = proPrice == null ? "0" : proPrice;
			map.put("userCode", userCode);
			map.put("productId", productId);
			map.put("allowReturn", allowReturn);
			map.put("brandCode", brandCode);
			map.put("corpId", corpId);
			map.put("shopId", shopId);
			map.put("productName", postName);
			map.put("categoryCode", categoryCode);
			map.put("typeCode", shopTypeCode);
			map.put("productDesc", postDesc);
			postDesc = postDesc == null || "".equals(postDesc) ? "本公司供应，质量保证，欢迎咨询洽谈。" : postDesc;
			map.put("productUnit", measuring);
			unitWeight = unitWeight == null || "".equals(unitWeight) ? "0" : unitWeight;
			map.put("unitWeight", unitWeight);
			map.put("delDays", delDays);
			map.put("proStock", proStock);
			map.put("proPrice", proPrice);
			map.put("status", "0");
			map.put("buyerRange", buyerRange);
			infolog.info("PostTradeAction|saveProductInfo|map:"+map);
			List<Map<String, String>> listPara = new ArrayList<Map<String,String>>();
			String colNames = request.getParameter("colNames");//动态商品参数
			String[] colArr = colNames.split(",");
			String[] paraArr = request.getParameter("paraNames").split(",");
			String[] showArr = request.getParameter("paramShows").split(",");
			for(int i=1;i<colArr.length;i++){
				String para = colArr[i];
				Map<String, String> m = new HashMap<String, String>();
				String paraUuid = UUID.randomUUID().toString();
				m.put("paraUuid", paraUuid);
				m.put("corpId", corpId);
				m.put("shopId", shopId);
				m.put("productId", productId);
				m.put("paraName", paraArr[i]);
				String paraVal = request.getParameter(para);
				m.put("paraVal", paraVal);
				String paramShow = i>showArr.length-1 ? "0" : showArr[i];
				m.put("paramShow", paramShow);
				para = para.replace("att", "");
				String pUuid = request.getParameter("pUuid"+para);
				m.put("pUuid", pUuid);
				if("-1".equals(pUuid)) m.put("setType", "2");
				else m.put("setType", "1");
				m.put("status", "1");
				m.put("sortNum", i+"");
				infolog.info("PostTradeAction|saveProductInfo|listPara:"+m);
				listPara.add(m);
			}
			
			List<Map<String, String>> listKey = new ArrayList<Map<String,String>>();
			for(int i=1;i<=5;i++){
				Map<String, String> m = new HashMap<String, String>();
				String key = "keyWord"+i;
				String proKey = request.getParameter(key);
				m.put("proKey", proKey);//关键词内容
				String keyUuid = UUID.randomUUID().toString();
				m.put("keyUuid", keyUuid);
				m.put("corpId", corpId);
				m.put("shopId", shopId);
				m.put("productId", productId);
				m.put("status", "1");
				m.put("sortNum", i+"");
				listKey.add(m);
			}
			
			List<Map<String, String>> listBuy = new ArrayList<Map<String,String>>();
			for(int i=0;i<3;){
				String buyNum = request.getParameter("buyNum"+i);
				if(buyNum == null || "".equals(buyNum)) break;
				Map<String, String> m = new HashMap<String, String>();
				String uuid = UUID.randomUUID().toString();
				m.put("uuid", uuid);
				m.put("corpId", corpId);
				m.put("shopId", shopId);
				m.put("productId", productId);
				m.put("status", "1");
				m.put("purchaseLow", buyNum);
				String buyNum1 = "-1";//
				String type = "1";
				if((i+1)<3){
					buyNum1 = request.getParameter("buyNum"+(i+1));
					if(buyNum1 != null && !"".equals(buyNum1)){
						buyNum1 = Integer.valueOf(buyNum1)-1+"";
						type = "2";
					} else {
						buyNum1 = "-1";
					}
				}
				m.put("type", type);
				m.put("purchaseUp", buyNum1);
				String price = request.getParameter("price"+i);
				m.put("price", price);
				m.put("sortNum", ++i+"");
				listBuy.add(m);
			}
			
			List<Map<String, String>> listImg = new ArrayList<Map<String,String>>();
			String imgNum = request.getParameter("imgNum");
			infolog.info("PostTradeAction|saveProductInfo|imgNum:"+imgNum);
			if(null!=imgNum&&!"".equals(imgNum)){
				int[] arr = getIntArr(imgNum);
				for(int i=0;i<arr.length;i++){
					Map<String, String> m = new HashMap<String, String>();
					String uuid = UUID.randomUUID().toString();
					m.put("uuid", uuid);
					m.put("corpId", corpId);
					m.put("shopId", shopId);
					m.put("productId", productId);
					m.put("status", "1");
					String imgUrl = request.getParameter("pic"+arr[i]);
					m.put("proPic", imgUrl);
					m.put("sortNum", (i+1)+"");
					infolog.info("PostTradeAction|saveProductInfo|m:"+m);
					listImg.add(m);
				}
			}
			//商品销售区域
			List<Map<String, String>> areaList = new ArrayList<Map<String,String>>();
			if(null != areaCode && !"".equals(areaCode)){
				areaCode = areaCode.replace("null", "");
				String[] areaArr = areaCode.split(",");
				for(int i=0;i<areaArr.length;i++){
					Map<String, String> obj = new HashMap<String, String>();
					obj.put("corpId", corpId);
					obj.put("shopId", shopId);
					obj.put("productId", productId);
					obj.put("areaCode", areaArr[i]);
					obj.put("uuid", UUID.randomUUID().toString());
					areaList.add(obj);
				}
			}
			boolean result = postTradeService.saveProductInfo(map,listPara,listKey,listBuy,listImg,areaList);
			if(result){
				json.put("saveFlag", "1");
			} else {
				json.put("saveFlag", "0");
			}
			json.put("productId", productId);
		} catch (Exception e) {
			json.put("saveFlag", "0");
			e.printStackTrace();
			errlog.error("PostTradeAction|save：",e);
		}finally {
			str2Resp(json.toString());
		}
	}
	
	/**
	 * 商品发布成功后跳转页面
	 * @return
	 */
	public String toPostTradeThreePage(){
		getRequest().setAttribute("productId", productId);//商品编码传入页面
		return "toPostTradeThreePage";
	}
	
	/**
	 * 商品信息页，toProductInfoPage：详情；toProductUpdatePage：更新
	 * @return
	 */
	public String toProductInfoPage(){
		request = getRequest();
		String userCode=getUserCodeFromSession();
		UserInfoBean userInfo=getUserInfo(userCode);
		String corpId = userInfo.getCorpId();
		String shopId=userInfo.getShopId();
		String auditStatus = request.getParameter("auditStatus");	//审核商品状态
		auditStatus = auditStatus == null ? "" : auditStatus;
		String uuid = request.getParameter("uuid");					//审核商品id
		uuid = uuid == null ? "" : uuid;
		String infoFlag = request.getParameter("pageFlag");
		infoFlag = infoFlag == null ? "" : infoFlag;
		request.setAttribute("infoFlag", infoFlag);
		if(infoFlag.equals("copyProduct")){
			String newProductId = UUID.randomUUID().toString();
			HashMap<String, String> map = new HashMap<String, String>();
			map.put("corpId", corpId);
			map.put("shopId", shopId);
			map.put("newProductId", newProductId);
			map.put("productId", productId);
			boolean result = postTradeService.copyProduct(map);
			if(result){
				productId = newProductId;
			} 
			request.setAttribute("copyResult", result);
		}
		Map<String, Object> auditInfo = postTradeService.getAuditInfo(uuid);
		Map<String, String> map = new HashMap<String, String>();
		map.put("productId", productId);
		map.put("corpId", corpId);
		map.put("shopId", shopId);
		Map<String, Object> proInfo = postTradeService.getProInfo(map);
		List<Map<String, Object>> areaList = postTradeService.queryShopAreaList(map);
		String areaName = "";
		if(areaList.size() > 0){
			for (Map<String, Object> aMap : areaList) {
				areaCode += (String) aMap.get("AREA_CODE").toString() + ",";
				areaName += (String) aMap.get("ANAME").toString() + ",";
			}
			if(!"".equals(areaCode)){
				areaCode = areaCode.substring(0,areaCode.length() - 1);
				areaName = areaName.substring(0,areaName.length() - 1);
			}
		}
		String allowReturn = proInfo.get("allowReturn")==null ? "" : proInfo.get("allowReturn").toString();
		allowReturn = allowReturn.equals("1") ? "是" : "否";
		proInfo.put("allowReturn", allowReturn);
		String buyerRange = proInfo.get("buyerRange")==null ? "" : proInfo.get("buyerRange").toString();
		buyerRange = buyerRange.equals("1") ? "全部" : "自定义";
		proInfo.put("buyerRange", buyerRange);
		String itemName = postTradeService.getCascadeName((String)proInfo.get("categoryCode"));
		request.setAttribute("productId", productId);//商品编码传入页面
		request.setAttribute("pro", proInfo);
		request.setAttribute("itemName", itemName);
		request.setAttribute("auditStatus", auditStatus);
		request.setAttribute("uuid", uuid);
		request.setAttribute("auditInfo", auditInfo);
		request.setAttribute("areaName", areaName);
		
		if("info".equals(infoFlag)) return "toProductInfoPage";
		return "toProductUpdatePage";
	}
	
	/**
	 * 商品详情/更新页查询
	 */
	public void queryProductPageInfo(){
		JSONObject json = new JSONObject();
		try {
			Map<String, String> map = new HashMap<String, String>();
			map.put("productId", productId);
			map.put("itemCode", itemCode);
			List<Map<String, Object>> keyList = postTradeService.getKeyWord(map);
			List<Map<String, Object>> paraList = postTradeService.getProParam(map);
			List<Map<String, Object>> proPicList = postTradeService.getProPic(map);
			List<Map<String, Object>> priceList = postTradeService.getProPrice(map);
			json.put("keyList", keyList);
			json.put("paraList", paraList);
			json.put("proPicList", proPicList);
			json.put("priceList", priceList);
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("PostTradeAction|save：",e);
		} finally {
			str2Resp(json.toString());
		}
	}
	
	/**
	 * 商品信息更新
	 */
	public void updateInfo(){
		JSONObject json = new JSONObject();
		try {
			request = getRequest();
			Map<String, String> map = new HashMap<String, String>();
			String productId = request.getParameter("productId");//商品编码
			String userCode=getUserCodeFromSession();
			UserInfoBean userInfo=getUserInfo(userCode);
			String corpId = userInfo.getCorpId();
			String shopId=userInfo.getShopId();
			String postName = request.getParameter("postName");//商品名称
			String brandCode = request.getParameter("brandCode");//品牌名称
			String categoryCode = request.getParameter("itemCode");//商品类目
			String shopTypeCode = request.getParameter("shopTypeCode");//店内商品分类
			String postDesc = request.getParameter("postDesc");//商品描述
			String measuring = request.getParameter("measuring");//计价单位
			String unitWeight = request.getParameter("unitWeight");//单位重量
			String delDays = request.getParameter("sendDate");//发货日期
			String allowReturn = request.getParameter("allowReturn");//是否允许退换货
			String buyerRange = request.getParameter("buyerRange");//买家范围
			allowReturn = null != allowReturn && "是".equals(allowReturn) ? "1" : "0";
			buyerRange = null != buyerRange && "全部".equals(buyerRange) ? "1" : "2";
			String reg = "[\u4e00-\u9fa5]";
			delDays = Pattern.compile(reg).matcher(delDays).replaceAll("");
			String proStock = request.getParameter("proStock");//商品库存
			map.put("productId", productId);
			map.put("brandCode", brandCode);
			map.put("corpId", corpId);
			map.put("shopId", shopId);
			map.put("productName", postName);
			map.put("categoryCode", categoryCode);
			map.put("typeCode", shopTypeCode);
			map.put("productDesc", postDesc);
			postDesc = postDesc == null || "".equals(postDesc) ? "本公司供应，质量保证，欢迎咨询洽谈。" : postDesc;
			map.put("productUnit", measuring);
			unitWeight = unitWeight == null || "".equals(unitWeight) ? "0" : unitWeight;
			map.put("unitWeight", unitWeight);
			map.put("delDays", delDays);
			map.put("proStock", proStock);
			map.put("allowReturn", allowReturn);
			map.put("buyerRange", buyerRange);
			
			//保存商品参数
			List<Map<String, String>> listPara = new ArrayList<Map<String,String>>();
			String colNames = request.getParameter("colNames");//动态商品参数
			String[] colArr = colNames.split(",");
			String[] paraArr = request.getParameter("paraNames").split(",");
			String[] showArr = request.getParameter("paramShows").split(",");
			for(int i=1;i<colArr.length;i++){
				String para = colArr[i];
				Map<String, String> m = new HashMap<String, String>();
				String paraUuid = UUID.randomUUID().toString();
				m.put("paraUuid", paraUuid);
				m.put("corpId", corpId);
				m.put("shopId", shopId);
				m.put("productId", productId);
				m.put("paraName", paraArr[i]);
				String paramShow = i>showArr.length-1 ? "0" : showArr[i];
				m.put("paramShow", paramShow);
				String paraVal = request.getParameter(para);
				m.put("paraVal", paraVal);
				para = para.replace("att", "");
				String pUuid = request.getParameter("pUuid"+para);
				m.put("pUuid", pUuid);
				if("-1".equals(pUuid)) m.put("setType", "2");
				else m.put("setType", "1");
				m.put("status", "1");
				m.put("sortNum", i+"");
				listPara.add(m);
			}
			
			//保存关键词
			List<Map<String, String>> listKey = new ArrayList<Map<String,String>>();
			for(int i=1;i<=5;i++){
				Map<String, String> m = new HashMap<String, String>();
				String key = "keyWord"+i;
				String proKey = request.getParameter(key);
				m.put("proKey", proKey);//关键词内容
				String keyUuid = UUID.randomUUID().toString();
				m.put("keyUuid", keyUuid);
				m.put("corpId", corpId);
				m.put("shopId", shopId);
				m.put("productId", productId);
				m.put("status", "1");
				m.put("sortNum", i+"");
				listKey.add(m);
			}
			
			//保存价格
			List<Map<String, String>> listBuy = new ArrayList<Map<String,String>>();
			for(int i=0;i<3;){
				String buyNum = request.getParameter("buyNum"+i);
				if(buyNum == null || "".equals(buyNum)) break;
				Map<String, String> m = new HashMap<String, String>();
				String uuid = UUID.randomUUID().toString();
				m.put("uuid", uuid);
				m.put("corpId", corpId);
				m.put("shopId", shopId);
				m.put("productId", productId);
				m.put("status", "1");
				m.put("purchaseLow", buyNum);
				String buyNum1 = "-1";//
				String type = "1";
				if((i+1)<3){
					buyNum1 = request.getParameter("buyNum"+(i+1));
					if(buyNum1 != null && !"".equals(buyNum1)){
						buyNum1 = Integer.valueOf(buyNum1)-1+"";
						type = "2";
					} else {
						buyNum1 = "-1";
					}
				}
				m.put("type", type);
				m.put("purchaseUp", buyNum1);
				String price = request.getParameter("price"+i);
				m.put("price", price);
				m.put("sortNum", ++i+"");
				listBuy.add(m);
			}
			
			//保存图片
			List<Map<String, String>> listImg = new ArrayList<Map<String,String>>();
			String imgNum = request.getParameter("imgNum");
			infolog.info("PostTradeAction|updateInfo|imgNum:"+imgNum);
			if(null!=imgNum&&!"".equals(imgNum)){
				int[] arr = getIntArr(imgNum);
				int j=0;
				for(int i=0;i<arr.length;i++){
					Map<String, String> m = new HashMap<String, String>();
					String uuid = UUID.randomUUID().toString();
					m.put("uuid", uuid);
					m.put("corpId", corpId);
					m.put("shopId", shopId);
					m.put("productId", productId);
					m.put("status", "1");
					String imgUrl = request.getParameter("pic"+arr[i]);
					m.put("proPic", imgUrl);
					m.put("sortNum", ++j+"");
					infolog.info("PostTradeAction|updateInfo|m:"+m);
					listImg.add(m);
				}
			}
			//商品销售区域
			List<Map<String, String>> areaList = new ArrayList<Map<String,String>>();
			if(null != areaCode && !"".equals(areaCode)){
				areaCode = areaCode.replace("null", "");
				String[] areaArr = areaCode.split(",");
				for(int i=0;i<areaArr.length;i++){
					Map<String, String> obj = new HashMap<String, String>();
					obj.put("corpId", corpId);
					obj.put("shopId", shopId);
					obj.put("productId", productId);
					obj.put("areaCode", areaArr[i]);
					obj.put("uuid", UUID.randomUUID().toString());
					areaList.add(obj);
				}
			}
			
			String result = postTradeService.updateInfo(map, listPara, listKey, listBuy, listImg,areaList);
			
			json.put("saveFlag", result);
			json.put("productId", productId);
		} catch (Exception e) {
			json.put("saveFlag", "0");
			e.printStackTrace();
			errlog.error("PostTradeAction|update：",e);
		}finally {
			str2Resp(json.toString());
		}
	}
	
	public void getTree(){
		request = getRequest();
		//id 异步加载展开树时传到后台的id
		//tarEnt 入口编码，以编码为入口编码的节点作为根节点
		//ifAll 是否一次性展示所有数据，1是 0否
//		String id = request.getParameter("id");
//		id = id == null ? "" : id;
//		String ifAll = request.getParameter("ifAll");
//		ifAll = ifAll == null ? "1" : ifAll;
//		SyncSelectTree selectTree = new SyncSelectTree();
//		selectTree.setId(id);
//		selectTree.setIfAll(ifAll);
		Map<String, String> map = new HashMap<String, String>();
		String userCode=getUserCodeFromSession();
		UserInfoBean userInfo=getUserInfo(userCode);
		String corpId = userInfo.getCorpId();
		String shopId=userInfo.getShopId();
		map.put("corpId", corpId);
		map.put("shopId", shopId);
		List<SyncSelectTree> list = postTradeService.getTree(map);
		System.out.println(list.size()+"========"+map);
		List<Map<String, Object>> checkList = postTradeService.queryShopAreaList(map);
		checkList = checkList == null ? new ArrayList<Map<String,Object>>() : checkList;
		for (SyncSelectTree sst : list) {
			sst.setOpen(true);
			if(sst.getIsParent() && "0".equals(sst.getpId())){
				sst.setNocheck(true);
			}
			String sstAreaCode = sst.getId();
			if(!checkList.isEmpty()){
				for (Map<String, Object> m : checkList) {
					String mAreaCode = (String) m.get("AREA_CODE") == null ? "" : m.get("AREA_CODE").toString();
					if(sstAreaCode.equals(mAreaCode)){
						sst.setChecked(true);
					}
				}
			}
		}
		str2Resp(JSONArray.fromObject(list).toString());
	}
	
	public int[] getIntArr(String args){
		String[] arr = args.split(",");
		int[] intArr = new int[arr.length-1];
		for(int i=1;i<arr.length;i++){
			intArr[i-1]=Integer.valueOf(arr[i]);
		}
		Arrays.sort(intArr);
		return intArr;
	}
	
	private String brandName;
	
	public void getBrandInfo(){
		HashMap<String, String> map = new HashMap<String, String>();
		map.put("brandName", brandName);
		List<Map<String, Object>> list = postTradeService.queryBrandInfoList(map);
		str2Resp(JSONArray.fromObject(list).toString());
	}
	
	public String getBrandName() {
		return brandName;
	}

	public void setBrandName(String brandName) {
		this.brandName = brandName;
	}

	public String getLevel() {
		return level;
	}

	public void setLevel(String level) {
		this.level = level;
	}

	public String getItemCode() {
		return itemCode;
	}

	public void setItemCode(String itemCode) {
		this.itemCode = itemCode;
	}

	public String getItemName() {
		return itemName;
	}

	public void setItemName(String itemName) {
		this.itemName = itemName;
	}

	public String getViewCode() {
		return viewCode;
	}

	public void setViewCode(String viewCode) {
		this.viewCode = viewCode;
	}

	public String getCodeName() {
		return codeName;
	}

	public void setCodeName(String codeName) {
		this.codeName = codeName;
	}

	public String getFlag() {
		return flag;
	}

	public void setFlag(String flag) {
		this.flag = flag;
	}

	public String getTypePCode() {
		return typePCode;
	}

	public void setTypePCode(String typePCode) {
		this.typePCode = typePCode;
	}

	public String getProductId() {
		return productId;
	}

	public void setProductId(String productId) {
		this.productId = productId;
	}

	public String getViewFlag() {
		return viewFlag;
	}

	public void setViewFlag(String viewFlag) {
		this.viewFlag = viewFlag;
	}

	public String getAreaCode() {
		return areaCode;
	}

	public void setAreaCode(String areaCode) {
		this.areaCode = areaCode;
	}

}
